package wec_wallet.ebo.com.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import bassproject.ebo.com.ebobass.refreshview.XRefreshView;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.Lg;
import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.R2;
import wec_wallet.ebo.com.adapter.BlackListAdapter;
import wec_wallet.ebo.com.bean.InterceptBean;
import wec_wallet.ebo.com.bean.InterceptDataBean;
import wec_wallet.ebo.com.common.Constant;
import wec_wallet.ebo.com.utils.Functions;

/**
 * 诈骗黑名单界面
 * Created by Administrator on 2018/3/14.
 */

public class BlackListActivity extends Activity {

    public static long lastRefreshTime;
    @BindView(R.id.lv_blackList)
    ListView lv;
    BlackListAdapter adapter;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindString(R2.string.blackList)
    String title;
    @BindView(R.id.custom_view)
    XRefreshView refreshView;
    private int pageNum = 1;
    private int listSize;
    private int limit = 10;
    private List<InterceptDataBean> lists;

    @OnClick(R.id.actionbar_back)
    public void back() {
        finish();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_black_list);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        tv_title.setText(title);
        adapter = new BlackListAdapter(this);
        lists = new ArrayList<>();
        lv.setAdapter(adapter);
        getData();

        // 设置是否可以下拉刷新
        refreshView.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        refreshView.setPullLoadEnable(true);
        // 设置上次刷新的时间
        refreshView.restoreLastRefreshTime(lastRefreshTime);
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        refreshView.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        refreshView.setAutoRefresh(false);
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                pageNum = 0;
                getData();

//                IToast.show("刷新完成");
            }

            @Override
            public void onLoadMore(boolean isSilence) {
//                IToast.show("加载完成");
                if (listSize == limit) {
                    getData();
                } else {
                    refreshView.stopLoadMore();

                    refreshView.stopRefresh();
                }
            }
        });

    }

    private void getData() {

        Map<String, Object> map = new HashMap<>();
        map.put("limit", limit + "");
        map.put("order_by", "1");
        map.put("page", pageNum + "");
        map.put("sort_field", "updated_at");
        map.put("token", I_Share.readAccountToken());
        map.put("uid", I_Share.readAccountUserid());
        Functions.post(BlackListActivity.this,  Constant.URL_BLACKLIST, map, new
                ListenerBlackList(), false, true);
    }

    private List<InterceptDataBean> getInterceptData(String json) {
        return new Gson().fromJson(json, InterceptBean.class).getData();

    }

    private class ListenerBlackList implements IHttpPostListener {
        @Override
        public void onTimeOut() {

        }
        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(result)) {
                return;
            }
            if (null != result) {
                try {
                    refreshView.stopLoadMore();

                    refreshView.stopRefresh();
                    JSONObject resultObject = new JSONObject(result);
                    String status = resultObject.getString("error");
                    if (resultObject.has("is_auth") && resultObject.getString("is_auth").equals
                            ("0")) {
                        IToast.show(resultObject.getString("error_msg"));
                        Functions.relogin(BlackListActivity.this);//登录过期

                        return;
                    }
                    if (resultObject.has("error_msg")) {
                        IToast.show(resultObject.getString("error_msg"));
                        return;
                    }
                    if (status.equals("0")) {
                        if (pageNum == 0) {
                            lists.clear();
                        }
                        List<InterceptDataBean> bean = getInterceptData(result);
                        listSize = bean.size();
                        if (bean.size() > 0) {
                            pageNum++;
                        }
                        lists.addAll(bean);
                        adapter.setLists(lists);
                        adapter.notifyDataSetChanged();

                    }

                } catch (Exception e) {
                    // TODO: handle exception
//                    IToast.show(e.toString());
                    Lg.d(getClass() + ",接口解析异常：\n" + e.toString());
                }
            } else {
                Lg.d("黑名单列表 result=null");

            }
        }
    }
}
